package com.lovo.homework;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Doudizhu extends JFrame {
    private List<Card> player1 = new ArrayList();
    private List<Card> player2 = new ArrayList();
    private List<Card> player3 = new ArrayList();
    private List<Card> base = new ArrayList();
//    private Image bg = Util.image("/images/bgddz.jpg");
    private Image image = Util.image("/images/bt.jpg");
    private Icon btnImg = Util.iconImg("/images/start.jpg");//发牌
    //定义一个54张牌的集合
    private List<Card> list = new ArrayList<>();
    //定义牌的数字
    private String[] points = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
    //定义花色
    private String[] types = {"♠", "♥", "♣", "♦"};


    public Doudizhu() {
        //背景
//        this.setGlassPane(new JPanel(){
//            @Override
//            protected void paintComponent(Graphics g) {
//                super.paintComponent(g);
//                g.drawImage(bg,0,0,1400,900,this);
//            }
//        } );

        this.init();
        this.xipai();
        this.fapai();
        //设置空布局
        this.setLayout(null);

        //发牌按钮图片
        JButton btnImages = new JButton(btnImg);
        btnImages.setBounds(625,400 , 134, 35);
        this.add(btnImages);

        //标题图片
        this.setIconImage(image);

        //玩家一
        int y = 600;
        for (int i = 16; i >= 0; i--) {
            Icon iconImg = Util.iconImg("/images/" + player1.get(i).getPointValue() + "_" + player1.get(i).getNum() + ".jpg");
            JLabel imgLabel1 = new JLabel(iconImg);
            imgLabel1.setBounds(85, y, 105, 150);
            this.add(imgLabel1);
            y -= 30;
        }

        //玩家二
        int x = 900;
        for (int i = 16; i >= 0; i--) {
            Icon iconImg = Util.iconImg("/images/" + player2.get(i).getPointValue() + "_" + player2.get(i).getNum() + ".jpg");
            JLabel imgLabel1 = new JLabel(iconImg);
            imgLabel1.setBounds(x, 700, 105, 150);
            this.add(imgLabel1);
            x -= 30;
        }

        //玩家三
        int y1 = 600;
        for (int i = 16; i >= 0; i--) {
            Icon iconImg = Util.iconImg("/images/" + player3.get(i).getPointValue() + "_" + player3.get(i).getNum() + ".jpg");
            JLabel imgLabel1 = new JLabel(iconImg);
            imgLabel1.setBounds(1200, y1, 105, 150);
            this.add(imgLabel1);
            y1 -= 30;
        }

        //底牌
        int x1 = 650;
        for (int i = 2; i >= 0; i--) {
            Icon iconImg = Util.iconImg("/images/" + base.get(i).getPointValue() + "_" + base.get(i).getNum() + ".jpg");
            JLabel imgLabel1 = new JLabel(iconImg);
            imgLabel1.setBounds(x1, 50, 105, 150);
            this.add(imgLabel1);
            x1 -= 30;
        }


        //设置宽高
        this.setTitle("斗地主");
        this.setLocationRelativeTo(null);
        this.setSize(1400, 900);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }


    //主函数
    public static void main(String[] args) {
        new Doudizhu();
    }

    // 初始化54张扑克牌
    public void init() {
        //先放52张普通牌
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < types.length; j++) {
                list.add(new Card(points[i], types[j], (i + 3), (j + 1)));
            }
        }
        //添加大小王
        list.add(new Card("小王", "", 16, 1));
        list.add(new Card("大王", "", 17, 1));
        System.out.println(list);
    }

    // 洗牌
    public void xipai() {
        Collections.shuffle(list);
        System.out.println(list);
    }

    // 发牌
    public void fapai() {
        //subList (int fromIndex, int toIndex) fromIndex （包含）和toIndex （不包括）
        //玩家1：0-17 玩家2:17-34 玩家3:34-51 底牌堆51-54
        player1 = list.subList(0, 17);
        player2 = list.subList(17, 34);
        player3 = list.subList(34, 51);
        base = list.subList(51, 54);
        //排序
        sort(player1);
        sort(player2);
        sort(player3);
        sort(base);
        //输出
        System.out.println("玩家a手牌：" + player1);
        System.out.println("玩家b手牌：" + player2);
        System.out.println("玩家c手牌：" + player3);
        System.out.println("底牌3张：" + base);
    }

    //排序
    public void sort(List<Card> list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card c1, Card c2) {
                if (c1.getPointValue() == c2.getPointValue()) {
                    return c2.getPointValue() - c1.getPointValue();
                } else {
                    return c2.getPointValue() - c1.getPointValue();
                }
            }
        });
    }
}

